package com.other.server;


import com.other.servlet.Servlet;

import java.io.IOException;
import java.net.Socket;

/**
 * 一个请求与响应 就一个此对象
 * @author Administrator
 *
 */
public class Dispatcher implements Runnable{
	private Socket client;
	private Request req;
	private Response rep;
	private int code=200;


	//两个常量
	Dispatcher(Socket client){
		try {
			this.client=client;
			this.req = new Request(client.getInputStream());
			this.rep = new Response(client.getOutputStream());
		} catch (IOException e) {
			code=500;
			return;
//			e.printStackTrace();
		}
	}





	@Override
	public void run() {
		try {

			Servlet serv = WebApp.getServlet(req.getUrl());
			if(null==serv){
				this.code=404; //找不到处理
			}else{
				serv.doService(req, rep);
			}
			rep.push(code);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				rep.push(500);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
